# -*-coding:utf-8-*-

from tests.apis.http import *
from tests.apis.base import BaseAPI


class Devices(BaseAPI):
    def __init__(self, *args, **kwargs):
        super(Devices, self).__init__(*args, **kwargs)

    def platform_create_device(self, *args, **kwargs):
        device_ids = kwargs.pop('device_ids')
        device_ids = ','.join(device_ids)
        response = http_post('platform/api/devices', kwargs, query='%s&device_ids=%s' % (self.query, device_ids))
        return self.assert_response_success_data(response)

    def platform_delete_device(self, *args, **kwargs):
        device_ids = kwargs.pop('device_ids')
        device_ids = ','.join(device_ids)
        response = http_delete('platform/api/devices', query='%s&device_ids=%s' % (self.query, device_ids))
        return self.assert_response_success_data(response)

    def platform_get_device_list(self, *args, **kwargs):
        response = http_get('platform/api/devices', self.query)
        return self.assert_response_success_data(response)

    def platform_get_device_list_count(self, *args, **kwargs):
        response = http_get('platform/api/devices/count', self.query)
        return self.assert_response_success_data(response)

    def platform_get_device_type(self, *args, **kwargs):
        response = http_get('platform/api/devices', self.query)
        return self.assert_response_success_data(response)

    def platform_get_device(self, *args, **kwargs):
        device_id = kwargs.pop('device_id')
        response = http_get('platform/api/devices/%s' % device_id, self.query)
        return self.assert_response_success_data(response)

    def create_device(self, *args, **kwargs):
        response = http_post('api/devices', kwargs, self.query)
        return self.assert_response_success_data(response)

    def update_device(self, *args, **kwargs):
        device_id = kwargs.pop('device_id')
        response = http_put('api/devices/%s' % device_id, kwargs, self.query)
        return self.assert_response_success_data(response)

    def get_device(self, *args, **kwargs):
        device_id = kwargs.pop('device_id')
        response = http_get('api/devices/%s' % device_id, self.query)
        return self.assert_response_success_data(response)

    def delete_device(self, *args, **kwargs):
        device_id = kwargs.pop('device_id')
        response = http_delete('api/devices/%s' % device_id, query=self.query)
        return self.assert_response_success_data(response)

    def get_device_list(self, *args, **kwargs):
        query = self.query_from_kwargs(**kwargs)
        response = http_get('api/devices', query)
        return self.assert_response_success_data(response)

    def get_device_entries(self, *args, **kwargs):
        device_id = kwargs.pop('device_id')
        response = http_get('api/devices/%s/entries' % device_id)
        return self.assert_response_success_data(response)
